package com.test.image.util;

import com.test.image.dto.ObjDTO;
import com.test.image.dto.TimeDTO;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author Moro
 * @Date 2021/10/31 21:05
 * @title ListUtil
 */
public class ListUtil {
    public static List<Integer> getSubList(int n, List<Integer> list) {
        Collections.shuffle(list);
        return list.subList(0, n);
    }
    private static List<ObjDTO> objDTOList(){
        List<ObjDTO> objDTOList = new ArrayList<>();
        for (int i = 1; i <= 16; i++) {
            if (i<=4||i>=13){
                objDTOList.add(new ObjDTO(i,1));
            }else {
                objDTOList.add(new ObjDTO(i,0));
            }
        }
        return objDTOList;
    }
    public static Map<String, Object> calculate(List<ObjDTO> list,List<TimeDTO> timeDTOS) {
        int count1 = 0,count2=0;
        int wholeDiff = 0,analysisDiff=0;
        List<ObjDTO> objDTOS = objDTOList();
        List<ObjDTO> collect = list.stream().sorted(Comparator.comparing(ObjDTO::getNum)).collect(Collectors.toList());
        for (int i = 0; i < timeDTOS.size(); i++) {
            TimeDTO timeDTO = timeDTOS.get(i);
            if (timeDTO.getNum()<=8){
                wholeDiff+=timeDTO.getDiff();
            }else {
                analysisDiff+=timeDTO.getDiff();
            }
        }
        for (int i = 0; i < collect.size(); i++) {
            ObjDTO objDTO = objDTOS.get(list.get(i).getNum() - 1);
            if (objDTO.getNum()<=8){
                if (objDTO.getVal()==list.get(i).getVal()){
                    count1++;
                }
            }else {
                if (objDTO.getVal().compareTo(list.get(i).getVal())==0){
                    count2++;
                }
            }
        }
        //格式化小数
        DecimalFormat df = new DecimalFormat("0.00");
        //准确率
        String whole = df.format((float)count1/3);
        String analysis = df.format((float)count2/3);
        //单位时间准确率
        String wholeRate = df.format((float)count1/wholeDiff*1000);
        String analysisRate = df.format((float)count2/analysisDiff*1000);
        //反应时间
        String wholeReactionTime = df.format((float)wholeDiff/3/1000);
        String analysisReactionTime = df.format((float)analysisDiff/3/1000);
        Map<String, Object> map = new HashMap<>();
        map.put("whole",whole);
        map.put("analysis",analysis);
        map.put("wholeRate",wholeRate);
        map.put("analysisRate",analysisRate);
        map.put("wholeReactionTime",wholeReactionTime);
        map.put("analysisReactionTime",analysisReactionTime);
        return map;
    }
}
